/*
 Developer: John Archer (john.archer.jpa@gmail.com)
 
 This file is part of VTBuilder.

 VTBuilder is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License.

 VTBuilder is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with VTBuilder.  If not, see <http://www.gnu.org/licenses/>.
 */
package core.tasks.pack;

import core.sb.s.Seq;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class SPartPack_T extends Thread {

    public ArrayList<Seq> pOut = new ArrayList<Seq>();
    private final CountDownLatch stopLatch;
    private List<Seq> pIn;
    private HashMap<String, ArrayList<Integer>> lib;
    private int lengthOfTemplate;
    private int kt;
    public SPartPack_T(CountDownLatch stopLatch, List<Seq> pIn, HashMap<String, ArrayList<Integer>> wm, int lengthOfTemplate, int kt) {
        this.stopLatch = stopLatch;
        this.pIn = pIn;
        this.lib = wm;
        this.lengthOfTemplate = lengthOfTemplate;
        this.kt = kt;
    }

    public void run() {
        partition();
        stopLatch.countDown();
    }

    private void partition() {
        String dna;
        for (int s = 0; s < pIn.size(); s++) {
            
            int kHit = 0;int kMiss = 0;
            
            dna = pIn.get(s).dna;
            
            //cut off the last 5%
            dna = dna.substring(0, (int)(dna.length()*0.95));
            
            int totalNoInS = dna.length() - 10;
            
            boolean inMissRun = false;
            int lengthOfMissRun = 0;
            ArrayList <Integer> runsOfMisses = new ArrayList();
            for (int k = 0; k < totalNoInS; k++) {
                if (lib.containsKey(dna.substring(k, k + 10))) {
                    kHit++;
                    if(inMissRun){
                        runsOfMisses.add(lengthOfMissRun);
                        lengthOfMissRun = 0;
                        inMissRun = false;
                    } 
                }
                else{
                    kMiss++;
                    if(!inMissRun){inMissRun = true;}
                    lengthOfMissRun++;
                }
            }
            
            if(inMissRun){runsOfMisses.add(lengthOfMissRun);}
            

            //the total misses are the sum of all miss runs
            //but subtract 9 from each to help normalize for start.
            //of single hit kmers i.e. a kmer will wibe out 10 sites for just a single mismatch on its own
            
            int totalSitesInMisRegion = 0;
            for(int r : runsOfMisses){
                if(r>=10){totalSitesInMisRegion+=(r-9);}
                else{
                    //start of seq
                    totalSitesInMisRegion +=r;
                }
            }
            
            int totalSitesInHit = totalNoInS - totalSitesInMisRegion;
            
            double percentHittinRegion = ((double)totalSitesInHit/totalNoInS) * 100;

            //to a percent
            //and use 90% of query length to allow for some end variation
            if(percentHittinRegion >= kt){
                pOut.add(pIn.get(s));
                pIn.set(s, null);
            }
        }
    }
}